import { userlist,usercount } from "@/request/api"
//封装接口类型
interface StateUser {
    userList:Array<object>
 }
interface StateSize{
    size:number
}
interface StateCount{
    count:number
}
interface StatePage{
    page:number
}

const state = {
    userList:[],
    size: 3,//显示条数
    page: 1,//当前页数
    count: 0,//总条数
}

const getters = {
    getUser(state:StateUser){
      return state.userList
    },
    getSize(state:StateSize) {
        return state.size
    },
    getCount(state:StateCount) {
        return state.count
    }
}

const mutations ={
    REQ_USERLIST(state:StateUser, payload:any) {
        state.userList = payload
    },
    REQ_COUNT(state:StateCount, payload:any) {
        state.count = payload
    },
    //同步修改页码事件
    REQ_PAGE(state:StatePage,payload:any){
        state.page = payload
    }

}

const actions ={
    getUserAction(context:any){
        userlist({
            size: context.state.size,
            page: context.state.page
        })
        .then(res=>{
            // console.log(res);
            context.commit("REQ_USERLIST",res.list)
            
        })
    },
      //获取总页数
      getUserCount(context:any) {
        usercount().then((res:any) => {
            // console.log(res,"请求的总页数");
            context.commit("REQ_COUNT", res.list[0].total)
        })
    },
    // 获取当前条数
    changePageActions(context:any,payload:any){
        //修改页码
        context.commit("REQ_PAGE",payload)
        //重新调用接口
        context.dispatch("getUserAction")
    }

}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced:true
}